package com.example.xjt.api;

import com.example.xjt.restful.ResultVO;
import com.example.xjt.safe.VerificationCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Slf4j
@RestController
public class TestController {

    @GetMapping("/api/anon")
    public ResultVO test01() {
        return ResultVO.ok("匿名访问成功");
    }

    @GetMapping("/api/user")
    public ResultVO test02() {
        return ResultVO.ok("USER用户访问成功");
    }

    @GetMapping("/api/admin")
    public ResultVO test03() {
        return ResultVO.ok("管理员用户访问成功");
    }

    @GetMapping("/verifyCode")
    public void verifyCode(HttpServletRequest request, HttpServletResponse resp) throws IOException {
        VerificationCode code = new VerificationCode();
        BufferedImage image = code.getImage();
        String text = code.getText();
        HttpSession session = request.getSession(true);
        session.setAttribute("verify_code", text);
        VerificationCode.output(image,resp.getOutputStream());
    }
}
